
/**
 *  SPI1 接口点亮LCM，LCD驱动IC: ILI9341
 *
 *  接线对照
 *  LCD:    VCC     GND     SCK         SDI         CS      DC(RS)          BLK
 *  EC600S: 3.3v    GND     SPI_SCK     SPI_MOSI    SPI_SS  GPIO121_3V3     GPIO122_3V3
 *  丝印:   J7_3V3  J7_GND  J4_13       J4_11       J4_10   J4_9            J4_8
 *
 *  其中 DC/RS BLK 可以自由切换GPIO
 */
 var spiRequire = require('spi');
 var gpio = require('gpio');
 var spi;
 var RS;
 var BLK;
 
 var LcdDevice = {
     width: 320,
     height: 240,
     wramcmd: 0x2c,
     setxcmd: 0x2a,
     setycmd: 0x2b
 }
 
 var spiInit = function () {
     spi = spiRequire.open({
         id: 'SPI1'
     });
 
     RS = gpio.open({
         id: 'LCD_RS'
     });
 
     BLK = gpio.open({
         id: 'LCD_BLK'
     });
 
     BLK.writeValue(1);
 }
 
 var lcdSpiWrite = function (d) {
     if (d == undefined) {
         return
     }
     spi.write(d);
 }
 
 var lcdWriteCmd = function (regval) {
     RS.writeValue(0);
     lcdSpiWrite(regval);
 }
 
 var lcdWriteData = function (data) {
     RS.writeValue(1);
     lcdSpiWrite(data);
 }
 
 var lcdWriteDoubleByte = function (data) {
     var txBuffer = new Array(2);
     txBuffer[0] = (data >> 8);
     txBuffer[1] = (data & 0xff);
 
     RS.writeValue(1);
     lcdSpiWrite(txBuffer);
 }
 
 var lcdWriteCmdData = function (com, dat) {
     lcdWriteCmd(com);
     lcdWriteData(dat);
 }
 
 function lcdWriteRamPrepare() {
     lcdWriteCmd([LcdDevice.wramcmd]);
 }
 
 function lcdSoftReset() {
     lcdWriteCmd([0x01]);
     sleepMs(50);
 }
 
 function lcdSetCursor(xPos, yPos) {
     lcdWriteCmd([LcdDevice.setxcmd]);
     lcdWriteDoubleByte(xPos);
     lcdWriteCmd([LcdDevice.setycmd]);
     lcdWriteDoubleByte(yPos);
 }
 
 function lcdSetDisplayDir() {
     lcdWriteCmd([LcdDevice.setxcmd]);
     lcdWriteDoubleByte([0]);
     lcdWriteDoubleByte([LcdDevice.width -1]);
     lcdWriteCmd([LcdDevice.setycmd]);
     lcdWriteDoubleByte([0]);
     lcdWriteDoubleByte([LcdDevice.height -1]);
     lcdWriteCmdData([0x36], [((0 << 7) | (1 << 6) | (1 << 5)) | 0x08]);
 }
 
 function lcdSetWindow(sx, sy, width, height) {
     width[0] = sx[0] + width[0] - 1;
     height[0] = sy[0] + height[0] - 1;
     lcdWriteCmd([LcdDevice.setxcmd]);
     lcdWriteDoubleByte(sx);
     lcdWriteDoubleByte(width);
     lcdWriteCmd([LcdDevice.setycmd]);
     lcdWriteDoubleByte(sy);
     lcdWriteDoubleByte(height);
 }
 
 function lcdScreenClear(color) {
     var totalPoint = LcdDevice.width * LcdDevice.height * 2;
     lcdSetWindow([0], [0], [LcdDevice.width], [LcdDevice.height]);
     lcdSetCursor([0], [0]);
     lcdWriteRamPrepare();
 
     const buf = new ArrayBuffer(totalPoint);
     const lcdTxBuffer = new Uint8Array(buf);
     for (var i = 0; i < totalPoint; i = i + 2) {
         lcdTxBuffer[i] = (color >> 8);
         lcdTxBuffer[i+1] =(color & 0xff);
     }
     console.log('start write data, high byte: '+ (color >> 8), 'low byte: '+ (color & 0xff));
     lcdWriteData(lcdTxBuffer);
 }
 
 var Lcd_Init = function () {
     console.log('lcd init start');
     spiInit();
     lcdSoftReset();
     sleepMs(50);
 
     lcdWriteCmd([0xCF]);
     lcdWriteData([0x00, 0xD9, 0X30]);
 
     lcdWriteCmd([0xED]);
     lcdWriteData([0x64, 0x03, 0X12, 0X81]);
 
     lcdWriteCmd([0xE8]);
     lcdWriteData([0x85, 0x10, 0x78]);
 
     lcdWriteCmd([0xCB]);
     lcdWriteData([0x39, 0x2C, 0x00, 0x34, 0x02]);
 
     lcdWriteCmd([0xF7]);
     lcdWriteData([0x20]);
 
     lcdWriteCmd([0xEA]);
     lcdWriteData([0x00, 0x00]);
 
     lcdWriteCmd([0xC0]);
     lcdWriteData([0x21]);
 
     lcdWriteCmd([0xC1]);
     lcdWriteData([0x12]);
 
     lcdWriteCmd([0xC5]);
     lcdWriteData([0x32, 0x3C]);
 
     lcdWriteCmd([0xC7]);
     lcdWriteData([0xC1]);
 
     lcdWriteCmd([0x36]);
     lcdWriteData([0x08]);
 
     lcdWriteCmd([0x3A]);
     lcdWriteData([0x55]);
 
     lcdWriteCmd([0xB1]);
     lcdWriteData([0x00, 0x18]);
 
     lcdWriteCmd([0xB6]);
     lcdWriteData([0x0A, 0xA2]);
 
     lcdWriteCmd([0xF2]);
     lcdWriteData([0x00]);
 
     lcdWriteCmd([0x26]);
     lcdWriteData([0x01]);
 
     lcdWriteCmd([0xE0]);
     lcdWriteData([0x0F, 0x20, 0x1E, 0x09, 0x12, 0x0B, 0x50, 0xBA, 0x44, 0x09, 0x14, 0x05, 0x23, 0x21, 0x00]);
 
     lcdWriteCmd([0xE1]);
     lcdWriteData([0x00, 0x19, 0x19, 0x00, 0x12, 0x07, 0x2D, 0x28, 0x3F, 0x02, 0x0A, 0x08, 0x25, 0x2D, 0x0F]);
 
     lcdWriteCmd([0x11]);    //Exit Sleep
     sleepMs(120);
     lcdWriteCmd([0x29]);    //Display on
 
     lcdSetDisplayDir();
     console.log('lcd start clear screen');
     BLK.writeValue(1);
     lcdScreenClear(0xF800)  //RED
     console.log('lcd init sucesss');
 }
 
 function lcdFill(sx, sy, ex, ey, color) {
     xlen = (ex - sx) * (ey - sy);
 
     lcdWriteCmd([LcdDevice.setxcmd]);
     lcdWriteDoubleByte(sx);
     lcdWriteDoubleByte(ex -1);
     lcdWriteCmd([LcdDevice.setycmd]);
     lcdWriteDoubleByte(sy);
     lcdWriteDoubleByte(ey -1);
     lcdWriteCmd([LcdDevice.wramcmd]);
 
     xlen = xlen * 2;
     for (i = 0; i < xlen; i = i + 2) {
         lcdTxBuffer[i] = color >> 8;
         lcdTxBuffer[i + 1] = color;
     }
     lcdWriteData(lcdTxBuffer);
     lcdWriteCmd([LcdDevice.setxcmd]);
     lcdWriteDoubleByte(0);
     lcdWriteDoubleByte(LcdDevice.width -1);
     lcdWriteCmd([LcdDevice.setycmd]);
     lcdWriteDoubleByte(0);
     lcdWriteDoubleByte(LcdDevice.height -1);
 }
 
 function lcdColorTest() {
     lcdScreenClear(0x07E0); //GREEN
     sleepMs(1000);
     lcdScreenClear(0xF800);
     sleepMs(1000);
 }
 
 Lcd_Init();
 setInterval(function() {
     console.log('lcd screen clear');
     lcdColorTest();
 }, 2200);
 